package cn.edu.ujn.park.controller;

import cn.edu.ujn.park.dao.Rate;
import cn.edu.ujn.park.dao.Vehicle;
import cn.edu.ujn.park.service.IRateService;
import cn.edu.ujn.util.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class RateController {
    @Autowired
    private IRateService iRateService;
    @GetMapping("/rate")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1")Integer page ,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows, @ModelAttribute Rate rate){
        rate.setStart((page-1)*rows);
       rate.setRows(rows);
        if("".equals(rate.getRateId())){
            rate.setRateId(null);
        }
        if("".equals(rate.getVehicleType())){
            rate.setVehicleType(null);
        }
        if("".equals(rate.getSpaceType())){
            rate.setSpaceType(null);
        }
        if("".equals(rate.getRatePerHour())){
            rate.setRatePerHour(null);
        }
        Page<Rate> page1 = iRateService.selectAll(rate);
        page1.setPage(page);
        model.addAttribute("page",page1);
        return "rate_manage";
    }    @GetMapping("/rateself")
    public String list2(Model model, @RequestParam(value = "page",defaultValue = "1")Integer page ,
                        @RequestParam(value = "row",defaultValue = "10") Integer rows, @ModelAttribute Rate rate, HttpServletRequest request){
       rate.setStart((page-1)*rows);
        rate.setRows(rows);
        Integer rateId = (Integer) request.getSession().getAttribute("rateId");
        rate.setRateId(rateId);
        Page<Rate> page1 = iRateService.selectAll(rate);
        page1.setPage(page);
        model.addAttribute("page",page1);
        return "rate_user";
    }


    @PostMapping("/rate")
    @ResponseBody
    public String createRate(Rate rate){

        iRateService.insert(rate);

        return "OK";
    }

    /**
     * 根据id 获取Vehicle对象
     * @param id
     * @return
     */
    @GetMapping("/rate/{id}")
    @ResponseBody
    public Rate editRate(@PathVariable("id") Integer id){
        Rate rate = iRateService.getRateById(id);
        return rate;
    }

    /**
     * 更新user
     * @param rate
     * @return
     */
    @PutMapping("/rate")
    @ResponseBody
    public String updateRate(@RequestBody Rate rate){

        iRateService.updateByPrimaryKey(rate);

        return "OK";
    }

    /**
     * 删除user
     * @param id
     * @return
     */

    @DeleteMapping("/rate/{id}")
    @ResponseBody
    public String deleteRate(@PathVariable("id") Integer id){

        iRateService.deleteByPrimaryKey(id);
        return "OK";
    }
}
